﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebsiteBanThucAn.Models;
using System.Data;
using WebsiteBanThucAn.DAL;

namespace DAL
{
    public class LoaiMonAnDAL
    {
        private static void GanDoiTuong(LoaiMonAnModel lmam, DataRow dr)
        {// Gán từng thuộc tính cho đối tượng
            lmam.LoaimonanID = int.Parse(dr[LoaiMonAnModel.loaimonanid].ToString());
            lmam.Tenloaimonan = dr[LoaiMonAnModel.tenloaimonan].ToString();
        }

        public static bool ThemLoaiMonAn(LoaiMonAnModel lmam)
        {
            bool result = false;
            try
            {
                string sql = string.Format("insert into {0} values ", LoaiMonAnModel.loaimonan);
                sql += string.Format("(N'{0}')", lmam.Tenloaimonan);
                int n = SqlDataProvider.ThucHienLenh(sql);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool XoaLoaiMonAn(int id)
        {
            bool result = false;
            try
            {
                string sql = string.Format("delete {0} where {1}='{2}'", LoaiMonAnModel.loaimonan, LoaiMonAnModel.loaimonanid, id);
                int n = SqlDataProvider.ThucHienLenh(sql);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool SuaLoaiMonAn(LoaiMonAnModel lmam)
        {
            bool result = false;
            try
            {
                string sql = string.Format("update {0} set {1}=N'{2}'", LoaiMonAnModel.loaimonan, LoaiMonAnModel.tenloaimonan, lmam.Tenloaimonan);
                sql += string.Format(" where {0}='{1}'", LoaiMonAnModel.loaimonanid, lmam.LoaimonanID);
                int n = SqlDataProvider.ThucHienLenh(sql);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
